﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Log;
using mepostras.BLL;
using mepostras.BO.Util;
using mepostras.DAL;
using mepostras.Settings.BusinessLayer;
using GeneralCode = mepostras.BO.Util.BoConfig.MessageCode.General;
namespace mepostras.BO
{
    public class FaqBo : IFaqBo
    {
        /// <summary>
        /// Decode để hiển thị ra Html
        /// </summary>
        /// <param name="faq"></param>
        /// <returns></returns>
        private void HtmlDecode(Faq faq)
        {
            if (faq != null)
            {
                faq.FullName = HttpUtility.HtmlDecode(faq.FullName);
                faq.Title = HttpUtility.HtmlDecode(faq.Title);
                faq.Email = HttpUtility.HtmlDecode(faq.Email);
                faq.Question = HttpUtility.HtmlDecode(faq.Question);
            }
        }

        /// <summary>
        /// Encode để hiển thị ra Html
        /// </summary>
        /// <param name="faq"></param>
        /// <returns></returns>
        private void HtmlEncode(Faq faq)
        {
            if (faq != null)
            {
                faq.FullName = HttpUtility.HtmlEncode(faq.FullName);
                faq.Title = HttpUtility.HtmlEncode(faq.Title);
                faq.Email = HttpUtility.HtmlEncode(faq.Email);
                faq.Question = HttpUtility.HtmlEncode(faq.Question);
            }
        }

        public bool Update(Faq faq, out string messageCode)
        {
            messageCode = GeneralCode.ServerError;
            bool result = false;
            try
            {
                faq.UpdatedDate = DateTime.Now;
                HtmlEncode(faq);
                result = DataRepository.FaqProvider.Update(faq);
                messageCode = result ? GeneralCode.UpdateSuccess : GeneralCode.UpdateFail;
            }
            catch (Exception ex)
            {
                SingletonLogger.Instance.Error("Update", ex);
            }
            return result;
        }

        public bool Insert(Faq faq, out string messageCode)
        {
            messageCode = GeneralCode.ServerError;
            bool result = false;
            try
            {
                faq.CreatedDate = faq.UpdatedDate = DateTime.Now.Date;

                HtmlEncode(faq);

                result = DataRepository.FaqProvider.Insert(faq);

                // Dựa vào kết quả trả về mà set mã lỗi
                messageCode = result ? GeneralCode.InsertSuccess : GeneralCode.InsertFail;
            }
            catch (Exception ex)
            {
                SingletonLogger.Instance.Error("Insert", ex);
            }
            return result;
        }

        public bool Book(Faq faq, out string messageCode)
        {
            if (Insert(faq, out messageCode))
            {
                messageCode = BoConfig.MessageCode.Contact.ContactSuccess;
                return true;
            }

            messageCode = BoConfig.MessageCode.Contact.ContactFail;
            return false;
        }

        public bool Delete(int id, out string messageCode)
        {
            messageCode = GeneralCode.ServerError;
            bool result = false;
            try
            {
                result = DataRepository.FaqProvider.Delete(id);
                messageCode = result ? GeneralCode.DeleteSuccess : GeneralCode.DeleteFail;
            }
            catch (Exception ex)
            {
                SingletonLogger.Instance.Error("Delete", ex);
            }
            return result;
        }
    }
}
